<?php

namespace App\Http\Requests;

use App\Tool\Helper;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class GoodsAdd extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'money' => [
                'required', function($attribute, $value, $fail) {
                    if ($value < 1) {
                        $fail('提现金额不能小于1');
                        return;
                    }
                }
            ],
            'type' => [
                'required', function($attribute, $value, $fail) {
                    if (!in_array($value, [1, 2, 3])) {
                        $fail('提现类型错误');
                        return;
                    }
                }
            ],
            'type_name' => 'required',
            'account' => 'required',
        ];
    }

    public function messages()
    {
        return [
            'money.required' => '请输入提现金额',
            'type.required' => '请选择提现类型',
            'type_name.required' => '缺少参数提现类型名称',
            'account.required' => '请输入提现账号',
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        throw new HttpResponseException(response()->json([
            "code"  => 500,
            "msg"   => $validator->getMessageBag()->first(),
            "data"  => null,
        ], 200, [
            'Content-Type'=>'application/json; charset=UTF-8'
        ]));
    }
}
